package main.util;


import main.pojo.Dish;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 功能类
 */
public class FunctionUtil {
    //全局变量type
    int type = -1;

    /**
     * 注册：
     * 查询重复
     * 插入
     */
    public static void register(int type) {
        System.out.println("---------蟹堡王欢迎您！！------");
        String tableName = getTableName(type);
        //判断用户名是否已存在，应当唯一
        try (Connection conn = DBUtils.getConn()) {
            Statement s = conn.createStatement();
            String count = "";
            String loginName = "";
            String sql = "";
            do {
                loginName = InputUtil.inputString("用户名,必填!");
                sql = "SELECT COUNT(*) COUNT FROM " + tableName + " WHERE LOGIN_NAME = '" + loginName + "'";
                s = conn.createStatement();
                ResultSet rs = s.executeQuery(sql);
                while (rs.next()) {
                    count = rs.getString("count");
                }
                if (!count.toString().equals("0")) {
                    System.out.println("用户名已存在！！请重新输入");
                }
            } while (!count.toString().equals("0"));
            String passWord = InputUtil.inputString("密码,必填!");
            String name = InputUtil.inputString("姓名,必填!");
            int gender = InputUtil.inputInt("性别   0：男   1：女    2：保密,必填!");
            String email = InputUtil.inputString("邮箱");
            String mobile = InputUtil.inputString("手机号");
            sql = "INSERT INTO " + tableName + " VALUES( SYS_GUID(),'" + loginName + "','" + passWord + "','" + name + "','" +
                    gender + "','" + email + "','" + mobile + "','1','" + loginName + "'," + " SYSDATE,'" + loginName + "',SYSDATE)";

            if (type == 0) {
                String address = InputUtil.inputString("地址");
                sql = "INSERT INTO " + tableName + " VALUES( SYS_GUID(),'" + loginName + "','" + passWord + "','" + name + "','" +
                        gender + "','" + email + "','" + mobile + "','1','" + loginName + "'," + " SYSDATE,'" + loginName + "',SYSDATE,'" + address + "')";

            }


            s.executeUpdate(sql);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    /**
     * 登录：
     * 查询账号密码正确性
     */
    public static String login(int type) {
        System.out.println("---------蟹堡王欢迎您！！------");
        String tableName = getTableName(type);
        String loginName = "";
        try (Connection conn = DBUtils.getConn()) {
            Statement s = conn.createStatement();
            String count = "";
            String sql = "";
            do {
                loginName = InputUtil.inputString("用户名");
                String passWord = InputUtil.inputString("密码");
                sql = "SELECT COUNT(*) COUNT FROM " + tableName + " WHERE LOGIN_NAME = '" + loginName + "' AND PASSWORD = '" + passWord + "'";
                s = conn.createStatement();
                ResultSet rs = s.executeQuery(sql);
                while (rs.next()) {
                    count = rs.getString("count");
                }
                if (count.toString().equals("0")) System.out.println("用户名或密码错误！！");
            } while (count.toString().equals("0"));
            return loginName;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return loginName;
    }


    /**
     * 首页
     *
     * @param type
     * @param loginName
     */
    public static void goToInex(int type, String loginName) {
        System.out.println("-----------首页-------------------");
        int num1 = InputUtil.inputSwitch(new String[]{"门店信息", "商户信息", "个人信息查看", "个人信息修改", "特有功能"});
        if (num1 == 0) gotoStores(type, loginName, 1);
        if (num1 == 1) gotoMerchants(type, loginName, 1);
        // nuber:判断来源
        if (num1 == 2) getUserInfo(loginName, type, 0);
        if (num1 == 3) updateMyInfo(type, loginName);
        if (num1 == 4) mine(type, loginName, 1);
    }

    /**
     * 特有功能
     *
     * @param type      判断是否是
     * @param loginName
     * @param page
     */
    private static void mine(int type, String loginName, int page) {
        if (type == 0) {
            System.out.println("您的特有功能是查看个人店铺列表，即将跳转...");
            switch (type) {
                case 0:
                    int num2 = InputUtil.inputSwitch(new String[]{"查看个人店铺列表", "申请个人店铺"});
                    if (num2 == 0) {
                        //查看个人店铺列表
                        goToMystors(4, loginName, page);
                        break;
                    } else {
                        //申请个人店铺
                        goToApplyStors(4, loginName);
                        break;
                    }

            }
        } else {
            System.out.println("抱歉您没有特有功能，即将跳转...");
            goToInex(type, loginName);
        }

    }

    /**
     * 获取我的店铺列表
     *
     * @return
     */
    private static Map<String, Object> goToMystors(int type, String loginName, int page) {
        System.out.println("-------------个人店铺列表---------------------");
        String sql = "";
        int num = -1;
        String name = "";
        String workTime = "";
        String feature = "";
        String id = "";
        String proprietor = "";
        String address = "";
        String phone = "";
        String score = "";
        String introduce = "";
        String creator = "";
        String create_date = "";
        String updater = "";
        String update_date = "";
        String operator = "";
        String status = "";
        String total = "";
        String get = "";
        String used = "";

        try (Connection conn = DBUtils.getConn()) {
            Statement s = conn.createStatement();
            sql = "SELECT B.WORKTIMESTART||'~'|| B.WORKTIMEEND AS WORKTIME,B.* FROM T_STORES B JOIN T_MERCHANTS A ON A.LOGIN_NAME = B.OPERATOR WHERE  B.STATUS = 1 ORDER BY B.CREATE_DATE DESC";
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()) {
                id = rs.getString("ID");
                name = rs.getString("NAME");
                workTime = rs.getString("WORKTIME");
                proprietor = rs.getString("PROPRIETOR");
                address = rs.getString("ADDRESS");
                phone = rs.getString("PHONE");
                score = rs.getString("SCORE");
                feature = rs.getString("FEATURE");
                introduce = rs.getString("INTRODUCE");
                creator = rs.getString("CREATOR");
                create_date = rs.getString("CREATE_DATE");
                updater = rs.getString("UPDATER");
                update_date = rs.getString("UPDATE_DATE");
                operator = rs.getString("OPERATOR");
                status = rs.getString("STATUS");
                total = rs.getString("TOTAL");
                get = rs.getString("GET");
                used = rs.getString("USED");
                show(new String[]{"ID", "店名", "经营者", "营业时间", "地址", "固话", "评分", "特色", "介绍", "创建者", "创建时间", "更新者", "更新时间", "店主", "状态 1、申请  2、营业  3、封禁  4停业", "总数", "已领张数", "已使用张数"}, new String[]{id, name, proprietor, address, phone, workTime, score, feature, introduce, creator, create_date, updater, update_date, operator, status, total, get, used});
            }
            //修改个人店铺 返回首页
            num = InputUtil.inputSwitch(new String[]{"修改个人店铺", "返回首页"});
            if (num == 0) UpdateMystors(type, loginName);
            if (num == 1) goToInex(type, loginName);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    /**
     * 商家修改个人商店信息
     *
     * @param type
     * @param loginName
     */
    public static void UpdateMystors(int type, String loginName) {
        System.out.println("-----------个人商店信息修改------------------");
        System.out.println("仅支持修改：店名、经营者、地址、固话、店主！若部分修改请未修改部分与原数据填写一致");
        System.out.println("当前信息为:");
        String tableName = getTableName(type);
        String proprietor = "";
        //判断用户名是否已存在，应当唯一
        try (Connection conn = DBUtils.getConn()) {
            Statement s = conn.createStatement();
            String sql = "";
            String name = InputUtil.inputString("店名,必填!");
            String address = InputUtil.inputString("地址");
            String phone = InputUtil.inputString("固话");
            String operator = InputUtil.inputString("店主");

            sql = "UPDATE  " + tableName + " SET  NAME = '" + name + "',ADDRESS='" + address + "',PHONE = '" + phone + "',OPERATOR = '" + operator + "' WHERE PROPRIETOR = '" + loginName + "'";
            s.executeUpdate(sql);
            System.out.println("信息修改成功！即将跳转信息查看");
            goToMystors(type, loginName, 0);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 申请个人商铺
     *
     * @param type
     * @param loginName
     * @param
     */
    private static void goToApplyStors(int type, String loginName) {
        System.out.println("-------------申请个人商铺---------------------");
        String sql = "";
        int num = -1;
        String name = InputUtil.inputString("店名,必填!");
        String address = InputUtil.inputString("地址");
        String phone = InputUtil.inputString("固话");
        String proprietor = InputUtil.inputString("经营者");
        String operator = InputUtil.inputString("店主");
//todo
        sql = "INSERT INTO T_STORES (ID, NAME, PROPRIETOR, ADDRESS, PHONE, WORKTIMESTART, WORKTIMEEND, SCORE, FEATURE, INTRODUCE, CREATOR, CREATE_DATE, UPDATER, UPDATE_DATE, OPERATOR, STATUS, TOTAL, GET, USED) VALUES (sys_guid(),'" + name + "','" + proprietor + "','" + address + "','" + phone + "',NULL,NULL, NULL,NULL ,NULL,NULL,NULL,NULL,NULL,'" + operator + "',NULL,NULL,NULL,NULL);";
        System.out.println(sql);
        try (Connection conn = DBUtils.getConn()) {
            Statement s = conn.createStatement();
            s.executeUpdate(sql);
            System.out.println("-----申请完成，即将跳转首页----");
            goToInex(type, loginName);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }


    /**
     * 修改个人信息
     */
    public static void updateMyInfo(int type, String loginName) {
        System.out.println("-----------个人信息修改------------------");
        System.out.println("仅支持修改：密码、姓名、性别、邮箱、手机号！若部分修改请未修改部分与原数据填写一致");
        System.out.println("当前信息为:");
        Map<String, Object> map = getUserInfo(loginName, type, 1);
        String tableName = getTableName(type);
        //判断用户名是否已存在，应当唯一
        try (Connection conn = DBUtils.getConn()) {
            Statement s = conn.createStatement();
            String sql = "";
            String passWord = InputUtil.inputString("密码,必填!");
            String name = InputUtil.inputString("姓名,必填!");
            int gender = InputUtil.inputInt("性别   0：男   1：女    2：保密,必填!");
            String email = InputUtil.inputString("邮箱");
            String mobile = InputUtil.inputString("手机号");
            sql = "UPDATE  " + tableName + " SET  PASSWORD = '" + passWord + "',NAME = '" + name + "',GENDER='" + gender + "',EMAIL = '" + email + "',MOBILE = '" + mobile + "' WHERE LOGIN_NAME = '" + loginName + "'";
            s.executeUpdate(sql);
            System.out.println("信息修改成功！即将跳转信息查看");
            getUserInfo(loginName, type, 0);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 根据条件获取字符串
     *
     * @param type
     * @return
     */
    static String getTableName(int type) {
        String tableName = "";
        switch (type) {
            case 0:
                tableName = "T_MERCHANTS";
                break;
            case 1:
                tableName = "T_USER";
                break;
            case 2:
                tableName = "T_SYS_USER";
                break;
            case 3:
                tableName = "T_SYS_USER";
                break;
            case 4:
                tableName = "T_STORES";
                break;
        }
        return tableName;
    }

    /**
     * 性别转码
     *
     * @param GenderintId
     * @return
     */
    static String getGenderint(int GenderintId) {
        String genderint = "";
        switch (GenderintId) {
            case 0:
                genderint = "男";
                break;
            case 1:
                genderint = "女";
                break;
            case 2:
                genderint = "保密";
                break;
        }
        return genderint;
    }

    /**
     * 获取个人信息
     *
     * @param loginName
     * @param type
     * @param number    0 回首页  1 个人信息获取结束
     * @return
     */
    public static Map<String, Object> getUserInfo(String loginName, int type, int number) {
        System.out.println("-----------个人信息------------------");
        String sql = "";
        Map<String, Object> userInfo = new HashMap<>();
        String tableName = getTableName(type);
        try (Connection conn = DBUtils.getConn()) {
            Statement s = conn.createStatement();
            sql = "SELECT * FROM " + tableName + " WHERE LOGIN_NAME = '" + loginName + "'";
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()) {
                userInfo.put("id", rs.getString("ID"));
                userInfo.put("loginName", rs.getString("LOGIN_NAME"));
                userInfo.put("password", rs.getString("PASSWORD"));
                int num = rs.getInt("GENDER");
                userInfo.put("gender", getGenderint(num));
                userInfo.put("email", rs.getString("EMAIL"));
                userInfo.put("mobile", rs.getString("MOBILE"));
                userInfo.put("updateDate", rs.getString("UPDATE_DATE"));
            }
            System.out.println("登录名：" + userInfo.get("loginName") + '\t' + "密码" + userInfo.get("password"));
            System.out.println("性别：" + userInfo.get("gender") + '\t' + "邮箱" + userInfo.get("email"));
            System.out.println("手机号：" + userInfo.get("mobile") + '\t' + "最后修改时间" + userInfo.get("updateDate"));
            if (number != 1) {
                System.out.println("---------个人信息已展示，即将返回首页-----");
                goToInex(type, loginName);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return userInfo;
    }

    /**
     * 逛店铺
     * 列表展示
     * 查看详情
     */
    public static void gotoStores(int type, String loginName, int page) {
        System.out.println("-------------店铺---------------------");
        String sql = "";
        int num = -1;
        String name = "";
        String workTime = "";
        String feature = "";
        String id = "";

        try (Connection conn = DBUtils.getConn()) {
            Statement s = conn.createStatement();
            sql = "SELECT * FROM (SELECT ID,NAME,WORKTIMESTART||'~'||WORKTIMEEND AS WORKTIME,FEATURE FROM T_STORES WHERE  STATUS = 2 ORDER BY CREATE_DATE DESC) WHERE ROWNUM <= 10 * " + page + " AND ROWNUM > (" + page + " - 1) * 10";
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()) {
                id = rs.getString("ID");
                name = rs.getString("NAME");
                workTime = rs.getString("WORKTIME");
                feature = rs.getString("FEATURE");
                show(new String[]{"ID", "店名", "营业时间", "特色"}, new String[]{id, name, workTime, feature});
            }
            //翻页 退出  返回首页
            num = InputUtil.inputSwitch(new String[]{"上一页", "下一页", "返回首页", "查看详情", "看菜单"});
            if (num == 0) getPreviousPage(type, loginName, page, 1);
            if (num == 1) getNextPage(type, loginName, page, 1);
            if (num == 2) goToInex(type, loginName);
            if (num == 3) {
                id = InputUtil.inputString("需要查看详情的id");
                selectStoresMore(type, loginName, id);
            }
            if (num == 4) getDish(type, loginName, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 获取菜单
     *
     * @param type
     * @param loginName
     * @param id
     */
    private static void getDish(int type, String loginName, String id) {

        System.out.println("-------------菜品---------------------");
        int num = -1;
        try (Connection conn = DBUtils.getConn()) {
            Statement s = conn.createStatement();
            // 默认展示全部菜品
            String sql = "SELECT * FROM T_DISH WHERE STORES ='" + id + "'";
            ResultSet rs = s.executeQuery(sql);
            // 创建一个Map来存储菜单分类和对应的美食列表
            Map<String, List<Dish>> categorizedDishes = new HashMap<>();
            while (rs.next()) {
                id = rs.getString("ID");
                String name = rs.getString("NAME");
                String categoryOne = rs.getString("CATEGORY_ONE");
                String price = rs.getString("PRICE");
                String description = rs.getString("DESCRIPTION");
                String status = rs.getString("STATUS");
                String sort = rs.getString("SORT");
                String creator = rs.getString("CREATOR");
                Date createDate = rs.getDate("CREATE_DATE"); // 注意，日期类型可能需要使用rs.getDate()
                String updater = rs.getString("UPDATER");
                Date updateDate = rs.getDate("UPDATE_DATE"); // 同样，这里也可能需要使用rs.getDate()
                String categoryTwo = rs.getString("CATEGORY_TWO");
                String stores = rs.getString("STORES");
                // 创建Dish对象
                Dish dish = new Dish(id, name, categoryOne, price, description, status, sort, creator, createDate, updater, updateDate, categoryTwo, stores);
                // 将Dish对象添加到相应的分类列表中
                List<Dish> dishesForCategory = categorizedDishes.computeIfAbsent(categoryOne, k -> new ArrayList<>());
                dishesForCategory.add(dish);
            }

            // 3. 按一级分类打印菜单
            for (Map.Entry<String, List<Dish>> entry : categorizedDishes.entrySet()) {
                System.out.println("－－－－－－ " + entry.getKey() + "－－－－");
                int count = 0;
                for (Dish dish : entry.getValue()) {
                    System.out.print(dish.getName() + " "); // 这里假设Dish类有一个getName()方法来获取菜品名称
                    count++;
                    if (count % 3 == 0) {
                        System.out.println(); // 换行
                    }
                }
                System.out.println("－－－－－－－－－－－－－－－－－－－－－－－－－－");
            }
            //翻页 退出  返回首页
            num = InputUtil.inputSwitch(new String[]{"返回首页", "查看详情"});
            if (num == 0) goToInex(type, loginName);
            // todo  if (n1um == 1) selectDishMore(type,loginName,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 查看店铺详情
     *
     * @param id
     */
    private static void selectStoresMore(int type, String loginName, String id) {
        String sql = "";
        int num = -1;
        try (Connection conn = DBUtils.getConn()) {
            Statement s = conn.createStatement();
            sql = "SELECT A.LOGIN_NAME AS SJ_LOGIN_NAME ,A.NAME AS SJ_NAME, A.GENDER AS SJ_GENDER ,A.EMAIL  AS SJ_EMAIL,MOBILE AS SJ_MOBILE " +
                    ",B.ID,B.NAME,B.PROPRIETOR,B.ADDRESS,B.PHONE,B.WORKTIMESTART,B.WORKTIMEEND,B.SCORE,B.FEATURE,B.INTRODUCE,B.CREATOR,B.CREATE_DATE,B.UPDATER,B.UPDATE_DATE,B.OPERATOR,B.STATUS,B.TOTAL,B.GET,B.USED" +
                    " FROM T_MERCHANTS A JOIN T_STORES B ON A.LOGIN_NAME = B.OPERATOR " +
                    "WHERE A.ID = '" + id + "'" +
                    "ORDER BY A.UPDATE_DATE";
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()) {
                //商户
//                String sjLoginName = rs.getString("SJ_LOGIN_NAME");
//                String sjName = rs.getString("SJ_NAME");
//                int sjGenderId = rs.getInt("SJ_GENDER");
//                String sjGender = getGenderint(sjGenderId);
//                String sjEmail = rs.getString("SJ_EMAIL");
//                String sjMobile = rs.getString("SJ_MOBILE");
                //店铺
                String name = rs.getString("NAME");
                String proprietor = rs.getString("PROPRIETOR");
                String address = rs.getString("ADDRESS");
                String phone = rs.getString("PHONE");
                String worktimestart = rs.getString("WORKTIMESTART");
                String worktimeend = rs.getString("WORKTIMEEND");
                String score = rs.getString("SCORE");
                String feature = rs.getString("FEATURE");
                String introduce = rs.getString("INTRODUCE");
                String creator = rs.getString("CREATOR");
                String create_date = rs.getString("CREATE_DATE");
                String operator = rs.getString("OPERATOR");
                int statusId = rs.getInt("STATUS");
                //店铺状态转码
                String status = getStatus(statusId, 1);
                String total = "" + rs.getInt("TOTAL");
                String get = "" + rs.getInt("GET");
                String used = "" + rs.getInt("USED");
                show(
                        new String[]{"编号", "店铺名称", "经营者", "地址", "电话", "营业开始时间", "打样时间", "评分", "特点", "介绍", "创建者", "创建日期", "店主", "状态", "优惠券总数", "已领取", "被使用"},
                        new String[]{id, name, proprietor, address, phone, worktimestart, worktimeend, score, feature, introduce, creator, create_date, operator, status, total, get, used}
                );
            }
            num = InputUtil.inputSwitch(new String[]{"返回上级", "查看菜单", "评论", "评分(100分制)"});
            if (num == 0) gotoStores(type, loginName, 1);
            if (num == 1) {
                getDish(type, loginName, id);
            }
            if (num == 2) gotoComment(type, loginName, id);
            if (num == 3) setScore(type, loginName, id);


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 评分
     *
     * @param type
     * @param loginName
     * @param id
     */
    private static void setScore(int type, String loginName, String id) {
        System.out.println("-------评分-------------");
        String sql = "";
        int score = InputUtil.inputInt("您的评分，满分100分");
        sql = "INSERT INTO T_SCORE (ID, SCORE, OBJ, TYPE, STORE, CREATOR, CREATE_DATE, UPDATER, UPDATE_DATE, DITH) VALUES (sys_guid(),'" + score + "', '" + id + "', NULL, NULL, '" + loginName + "', SYSDATE, '" + loginName + "', SYSDATE, NULL);";
        try (Connection conn = DBUtils.getConn()) {
            Statement s = conn.createStatement();
            s.executeUpdate(sql);
            System.out.println("-----评分完成，即将跳转首页----");
            goToInex(type, loginName);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 评论列表
     *
     * @param type
     * @param loginName
     * @param id
     */
    private static void gotoComment(int type, String loginName, String id) {
        System.out.println("--------------门店评论列表-----------");
        String sql = "";
        int num = -1;
        try (Connection conn = DBUtils.getConn()) {
            Statement s = conn.createStatement();
            sql = "SELECT ID,'COMMENT',OBJ,'TYPE',CREATE_DATE,REPLY,DISH,STORE FROM T_COMMENT";
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()) {
                id = rs.getString("ID");
                String comment = rs.getString("COMMENT");
                int obj = rs.getInt("OBJ");
                // 评论对象转码
                String obj1 = getStatus(obj, 2);
                int type1 = rs.getInt("TYPE");
                // 评论类型转码
                String type2 = getStatus(obj, 3);
                String store = rs.getString("STORE");
                String creator = rs.getString("CREATOR");
                String createDate = rs.getString("CREATE_DATE");
                String reply = rs.getString("REPLY");
                String dish = rs.getString("DISH");
                show(new String[]{"ID", "商户名字", "商户地址", "追评/回复对象Id", "评论菜品"}, new String[]{id, comment, obj1, type2, creator, createDate, store, reply, dish});
            }
            //翻页 退出  返回首页
            num = InputUtil.inputSwitch(new String[]{"返回首页", "回复(追评)"});
            if (num == 0) goToInex(type, loginName);
            if (num == 1) {
                InputUtil.inputInt("您要评论的编号");
                comment(type, loginName, id);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 评论
     *
     * @param type
     * @param loginName
     * @param id
     */
    private static void comment(int type, String loginName, String id) {
        String sql = "";
        String xh = InputUtil.inputString("您追评/回复的编码");
        String comment = InputUtil.inputString("您评论的内容");
        try (Connection conn = DBUtils.getConn()) {
            Statement s = conn.createStatement();
            sql = "INSERT INTO T_COMMENT VALUES(sys_guid(),'" + comment + "',0,1,'" + loginName + "',SYSDATE,'" + loginName + "',SYSDATE,+" + id + "','" + xh + "')";
            s.executeUpdate(sql);
            System.out.println("-------评论已完成！即将跳转评论列表！-------------------");
            gotoComment(type, loginName, id);
            goToInex(type, loginName);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 看商户
     * 列表展示
     *
     * @param type
     * @param loginName
     */
    public static void gotoMerchants(int type, String loginName, int page) {
        System.out.println("-------------商户---------------------");
        String sql = "";
        int num = -1;
        String name = "";
        String address = "";
        String id = "";

        try (Connection conn = DBUtils.getConn()) {
            Statement s = conn.createStatement();
            sql = "SELECT * FROM (SELECT ID,LOGIN_NAME,ADDRESS FROM T_MERCHANTS WHERE  STATUS = 2 ORDER BY CREATE_DATE DESC) WHERE ROWNUM <= 5 * " + page + " AND ROWNUM > (" + page + " - 1) * 5";
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()) {
                name = rs.getString("NAME");
                address = rs.getString("ADDRESS");
                show(new String[]{"ID", "商户名字", "商户地址"}, new String[]{id, name, address});
                id = rs.getString("ID");
            }
            //翻页 退出  返回首页
            num = InputUtil.inputSwitch(new String[]{"上一页", "下一页", "返回首页", "查看详情"});
            if (num == 0) getPreviousPage(type, loginName, page, 2);
            if (num == 1) getNextPage(type, loginName, page, 2);
            if (num == 2) goToInex(type, loginName);
            if (num == 3) {
                id = InputUtil.inputString("您要查看的编号");
                selectMore(type, loginName, id);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 下一页
     *
     * @param type
     * @param loginName
     * @param page
     * @param num       店铺传1，商户传2
     */
    public static void getNextPage(int type, String loginName, int page, int num) {
        page++;
        switch (num) {
            case 1:
                gotoStores(type, loginName, page);
                break;
            case 2:
                gotoMerchants(type, loginName, page);
                break;
        }

    }

    /**
     * 上一页
     *
     * @param type
     * @param loginName
     * @param page
     * @param num       店铺传1，商户传2
     */
    private static void getPreviousPage(int type, String loginName, int page, int num) {
        page--;
        switch (num) {
            case 1:
                gotoStores(type, loginName, page);
                break;
            case 2:
                gotoMerchants(type, loginName, page);
                break;
        }
    }

    /**
     * 查看商户详情
     *
     * @param id
     */
    private static void selectMore(int type, String loginName, String id) {
        String sql = "";
        try (Connection conn = DBUtils.getConn()) {

            Statement s = conn.createStatement();
            sql = "SELECT A.LOGIN_NAME AS SJ_LOGIN_NAME ,A.NAME AS SJ_NAME, A.GENDER AS SJ_GENDER ,A.EMAIL  AS SJ_EMAIL,A.MOBILE AS SJ_MOBILE " +
                    ",B.ID,B.NAME,B.PROPRIETOR,B.ADDRESS,B.PHONE,B.WORKTIMESTART,B.WORKTIMEEND,B.SCORE,B.FEATURE,B.INTRODUCE,B.CREATOR,B.CREATE_DATE,B.UPDATER,B.UPDATE_DATE,B.OPERATOR,B.STATUS,B.TOTAL,B.GET,B.USED" +
                    " FROM T_MERCHANTS A JOIN T_STORES B ON A.LOGIN_NAME = B.OPERATOR " +
                    "  WHERE A.ID = '" + id + "'" +
                    "  ORDER BY A.UPDATE_DATE";
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()) {
                //商户
//                String sjLoginName = rs.getString("SJ_LOGIN_NAME");
                String sjName = rs.getString("SJ_NAME");
                int sjGenderId = rs.getInt("SJ_GENDER");
                String sjGender = getGenderint(sjGenderId);
                String sjEmail = rs.getString("SJ_EMAIL");
                String sjMobile = rs.getString("SJ_MOBILE");
                //店铺
                String name = rs.getString("NAME");
                String proprietor = rs.getString("PROPRIETOR");
                String address = rs.getString("ADDRESS");
                String phone = rs.getString("PHONE");
                String worktimestart = rs.getString("WORKTIMESTART");
                String worktimeend = rs.getString("WORKTIMEEND");
                String score = rs.getString("SCORE");
                String feature = rs.getString("FEATURE");
                String introduce = rs.getString("INTRODUCE");
                String creator = rs.getString("CREATOR");
                String create_date = rs.getString("CREATE_DATE");
                String operator = rs.getString("OPERATOR");
                int statusId = rs.getInt("STATUS");
                //店铺状态转码
                String status = getStatus(statusId, 1);
                String total = "" + rs.getInt("TOTAL");
                String get = "" + rs.getInt("GET");
                String used = "" + rs.getInt("USED");
                show(
                        new String[]{"商家名字", "商家性别", "商家电子邮件", "商家手机号", "店铺名称", "经营者", "地址", "电话", "营业开始时间", "打样时间", "评分", "特点", "介绍", "创建者", "创建日期", "店主", "状态", "优惠券总数", "已领取", "被使用"},
                        new String[]{sjName, sjGender, sjEmail, sjMobile, name, proprietor, address, phone, worktimestart, worktimeend, score, feature, introduce, creator, create_date, operator, status, total, get, used}
                );
                System.out.println("商家已查看即将返回首页...");
                goToInex(type, loginName);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 大横表展示
     *
     * @param name
     * @param value
     */
    static void show(String[] name, String[] value) {
        if (name.length != value.length) {
            throw new IllegalArgumentException("请传入等长数组");
        }

        for (int i = 0; i < name.length; i++) {
            System.out.print(name[i] + ":" + value[i] + "\t");
        }
        // 打印完所有元素后换行
        System.out.println();
    }

    /**
     * 转码
     *
     * @param statueId
     * @param type     用户转码传0； 店铺转码 传1； 评论对象 传2 评论类型 填3
     * @return
     */
    static String getStatus(int statueId, int type) {
        String status = "";
        // 用户转码： 状态  0：封禁    1：解禁
        if (type == 0) {
            switch (statueId) {
                case 0:
                    status = "封禁";
                    break;
                case 1:
                    status = "解禁";
                    break;
            }
        } else
            // 店铺转码 状态 1、申请  2、营业  3、封禁  4停业
            if (type == 1) {
                switch (statueId) {
                    case 1:
                        status = "申请";
                        break;
                    case 2:
                        status = "营业";
                        break;
                    case 3:
                        status = "封禁";
                        break;
                    case 4:
                        status = "停业";
                        break;
                }
            } else
                // 评论对象 0 菜品  1 门店
                if (type == 2) {
                    switch (statueId) {
                        case 0:
                            status = "菜品";
                            break;
                        case 1:
                            status = "门店";
                            break;
                    }
                } else
                    // 评论类型 0 评论 1 回复
                    if (type == 3) {
                        switch (statueId) {
                            case 0:
                                status = "评论";
                                break;
                            case 1:
                                status = "回复";
                                break;
                        }
                    }
        return status;
    }

}
